﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using AgencyEntityLib.BaseEntity;

using Ecopsy.Tools;
using OutdoorLib.BaseEntity.WS;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Dictionary;
using Grishko.UI;
using Grishko.DataTier.SqlSentence;
using System.Collections.Generic;
using OutdoorLib.BaseEntity.Filters;
using OutdoorLib.BaseEntity.Infrastructure;

public partial class Frame_Desktop: System.Web.UI.Page 
{
    


    protected void Page_Load(object sender, EventArgs e)
    {
        tvClients.BuildNode += new TreeControl._BuildNode(tvClients_BuildNode);
        tvContactFaces.BuildNode += new TreeControl._BuildNode(tvContactFaces_BuildNode);
        tvContracts.BuildNode += new TreeControl._BuildNode(tvContracts_BuildNode);
        tvConstractionShedule.BuildNode  += new TreeControl._BuildNode(tvConstractionShedule_BuildNode);        
    
        if (!IsPostBack)
        {
            BindTree();
        }
    }


    protected Control tvClients_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        return BuildNode(tree, nd,"ClientListFilter", "ListForm_Default");    
    }


    protected Control tvConstractionShedule_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        return BuildNode(tree, nd, "ConstractionSheduleFilter", "ListForm_SideShedule");    
    }
    
    protected Control tvContactFaces_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        return BuildNode(tree, nd, "CotactFaceListFilter", "ListForm_Default");    
    }
    
    protected Control tvContracts_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        return BuildNode(tree, nd, "ContractListFilter", "ListForm_Time");                
        
    }

    private Control BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd,string TagName, string ListFormUrl)
    {
        LinkButton btn = new LinkButton();
        btn.CssClass = tree.CssNode;
        btn.ID = nd.ID;
        btn.Text = (nd as DictionaryItem).Value.ToString();

        string[] param = { TagName , nd.ID,ListFormUrl};
        string sUrl = string.Empty;
        if (nd is TreeDictionaryItem.BranchItem)
        {

            sUrl = string.Format("{2}.aspx?ListTagName={0}&ID={1}", param);
        }
        else
        {
            
            sUrl = string.Format("{2}.aspx?ListTagName={0}", param);
        }

        string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode, nd.ID };
        if ((nd.ID.ToUpper().IndexOf("Proxy".ToUpper()) == -1))
            btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
        return btn;

    }

   

    private void BindTree()
    {
        tvClients.DataSource = ClientsFrame;
        tvContactFaces.DataSource = ContactFaces;
        tvContracts.DataSource = ContractList;
        tvConstractionShedule.DataSource = ConstractionSheduleList;
        DataBind();


    }

    protected TreeDictionaryItem ContractList
    {
        get
        {            
            return PopulateFilterDictionaryItem("Контракты","ContractListFilter" ) ;
        }
    }

    protected TreeDictionaryItem ConstractionSheduleList
    {
        get
        {
            return PopulateFilterDictionaryItem("Сетка загрузки", "ConstractionSheduleFilter");
        }
    }
    

    private TreeDictionaryItem PopulateFilterDictionaryItem(string Name,string Tag )
    {
        TreeDictionaryItem result = new TreeDictionaryItem() { ParamID = "Proxy_"+Tag+"_User", Name = Name };
        TreeDictionaryItem.BranchItem root = new TreeDictionaryItem.BranchItem() { ID = "Proxy_" + Tag + "_User", Value = "Фильтры пользователя" };
        PopulateBranch(root, Filter.ListEntityCategory.User, Tag);

        result.ChildNodes.Add(root);

        root = new TreeDictionaryItem.BranchItem() { ID = "Proxy_"+Tag+"_Common", Value = "Общие фильтры" };
        PopulateBranch(root, Filter.ListEntityCategory.Provider, Tag);
        PopulateBranch(root, Filter.ListEntityCategory.Client, Tag);

        result.ChildNodes.Add(root);
        return result;

    }

    private void PopulateBranch(TreeDictionaryItem.BranchItem root, Filter.ListEntityCategory Category, string TagName)
    {
        
        List <Filter> res= AppEnvironment.FilterSource.Get(TagName, Category);
        foreach (Filter i in res)
        { 
            root.ChildNodes.Add (new TreeDictionaryItem .BranchItem (){ID =i.ID,Value =i.Caption});
        }
        
    }


    protected TreeDictionaryItem ContactFaces
    {
        get 
        {
            return PopulateFilterDictionaryItem("Контактные лица", "CotactFaceListFilter");
            

        }
    }

    protected TreeDictionaryItem ClientsFrame
    {
        get 
        {
            return PopulateFilterDictionaryItem("Клиенты", "ClientListFilter");
        }
    }

    protected void OnRefresh(object sender, ImageClickEventArgs arg)
    {
        BindTree();
    }

}
